<?php

namespace App\Http\Controllers\Api;

use App\Common\Libs\ArrayHelper;
use App\Consts\GlobalCommentConst;
use App\Consts\GlobalConst;
use App\Exceptions\Error;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class CommentController extends BaseController
{
    public $validate = [
        "getList" => ["rules" => [
            "target_id" => "required|string|max:24",
        ]],
        "add" => ["rules" => [
            "target_id" => "required|string|max:24",
            "target_type" => "required|max:1",
            "content" => "required|string|max:900",
        ]],
        "del" => ["rules" => [
            "id" => "required|string|max:24",
        ]],
    ];

    /**
     * @path /comment/list
     * @category 评论
     * @title 列表
     * @method POST
     * @params [{"name":"target_id","type":"text","required":1,"example":"","desc":"主体ID"},{"name":"target_type","type":"text","required":1,"example":"","desc":"主体类型"},{"name":"order","type":"text","required":0,"example":"","desc":"按最新new【默认】，最热hot，发布者author排序"}]
     * @resBody {"success":true,"statusCode":200,"result":{"total_count":1,"page_size":10,"page":1,"list":[{"id":"24位评论ID","content":"评论内容","is_deleted":0,"is_like":false,"created_at":1599198791,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像","attention_status":"关注状态，1没关注，2关注了，3自己，4互关"},"count":{"reply":0,"like":0},"reply":{"total_count":0,"page_size":5,"page":1,"list":[{"id":"24位回复ID","content":"回复内容","is_deleted":0,"is_like":false,"created_at":1599198791,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像","attention_status":"关注状态，1没关注，2关注了，3自己，4互关"},"to_user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像","attention_status":"关注状态，1没关注，2关注了，3自己，4互关"},"count":{"reply":0,"like":0}}]}}]},"message":"OK"}
     */
    public function getList(Request $request)
    {
        $params = $request->all();

        $searchParams = [
            'is_view' => GlobalCommentConst::IS_VIEW,
            'current_user_id' => $this->getCurrentUserId(),
            'target_id' => $params['target_id'],
            'page' => $params['page'] ?? 1,
            'order' => $params['order'] ?? GlobalCommentConst::ORDER_NEW,
        ];
        $data = service()->CommentService->getCommentList($searchParams);

        return $this->apiReturn($data);
    }

    /**
     * @path /comment/add
     * @category 评论
     * @title 新增
     * @method POST
     * @params [{"name":"target_id","type":"text","required":1,"example":"","desc":"主体ID"},{"name":"target_type","type":"text","required":1,"example":"","desc":"主体类型"},{"name":"content","type":"text","required":1,"example":"","desc":"评论内容"}]
     * @resBody {"success":true,"statusCode":200,"result":{"total_count":1,"page_size":10,"page":1,"list":[{"id":"24位评论ID","content":"评论内容","is_deleted":0,"is_like":false,"created_at":1599198791,"user":{"id":"24位用户ID","nickname":"用户昵称","avatar":"头像","attention_status":"关注状态，1没关注，2关注了，3自己，4互关"},"count":{"reply":0,"like":0},"reply":{"total_count":0,"page_size":5,"page":1,"list":[]}}]},"message":"OK"}
     */
    public function add(Request $request)
    {
        $params = $request->all();
        $loadData = [
            'target_id' => $params['target_id'],
            'target_type' => $params['target_type'],
            'content' => $params['content'],
            'user_id' => $this->getCurrentUserId(),
        ];

        $commentInfo = service()->CommentService->addComment($loadData);
        if(!$commentInfo){
            return $this->apiReturn(Error::handle(lang('Note is disable')));
        }

        $result = ArrayHelper::merge($commentInfo, [
            'is_like' => 0,
            'count' => ['reply' => 0, 'like' => 0],
            'reply' => ['total_count' => 0, 'page_size' => GlobalConst::BASE_PAGE_COUNT, 'page' => 1, 'list' => []]
        ]);
        return $this->apiReturn($result);
    }

    /**
     * @path /comment/del
     * @category 评论
     * @title 删除
     * @method POST
     * @params [{"name":"id","type":"text","required":1,"example":"","desc":"评论ID"}]
     * @resBody {"success":true,"statusCode":200,"result":true,"message":"OK"}
     */
    public function del(Request $request)
    {
        $params = $request->all();

        $data = service()->CommentService->delComment($params['id'], $this->getCurrentUserId());

        return $this->apiReturn($data);
    }
}
